<?php
/**
 *   This file is part of GeoArticle (a Joomla plugin).
 *   Written by Ivar Bergman (www.ivarbergman.se)
 *   Copyright (c) 2010 Ivar Bergman. All rights reserved.
 *
 *   GeoArticle is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   GeoArticle is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with GeoArticle.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

  // Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );

class GeoArticleController extends JController
{
  function __construct($config = array())
  {
    parent::__construct($config);

    // Register Extra tasks
    $this->registerTask( 'save_tags', 'tags' );
  }


  // Task from the Article editor, to add geo tags to an article.
  function tags()
  {
    $document =& JFactory::getDocument();
    $document = JDocument::getInstance('raw');
    
    JRequest::setVar( 'view'  , 'articletags');
    JRequest::setVar( 'layout', $this->getTask() );
    parent::display();
  }

  function add()
  {
    JRequest::setVar( 'hidemainmenu', 1 );
    JRequest::setVar( 'layout', 'form'  );
    JRequest::setVar( 'view'  , 'geoarticletag');
    JRequest::setVar( 'edit', false );
    
    // Checkout the weblink
    $model = $this->getModel('geoarticletag');
    $model->checkout();

    parent::display();
  }
  
  function edit( )
  {
    JRequest::setVar( 'hidemainmenu', 1 );
    JRequest::setVar( 'layout', 'form'  );
    JRequest::setVar( 'view'  , 'geoarticletag');
    JRequest::setVar( 'edit', true );
    
    // Checkout the weblink
    $model = $this->getModel('geoarticletag');
    $model->checkout();
    
    parent::display();
  }
   
  function publish()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
      JError::raiseError(500, JText::_( 'Select an item to publish' ) );
    }

    $model = $this->getModel('geoarticletag');
    if(!$model->publish($cid, 1)) {
      echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
    }

    $this->setRedirect( 'index.php?option=com_geoarticle' );
  }


  function unpublish()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
      JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
    }

    $model = $this->getModel('geoarticletag');
    if(!$model->publish($cid, 0)) {
      echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
    }

    $this->setRedirect( 'index.php?option=com_geoarticle' );
  }

  function remove()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
      JError::raiseError(500, JText::_( 'Select an item to delete' ) );
    }

    $model = $this->getModel('geoarticletag');
    if(!$model->delete($cid)) {
      echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
    }

    $this->setRedirect( 'index.php?option=com_geoarticle' );
  }

  function save()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $post	= JRequest::get('post');
    $cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
    $post['id'] = (int) $cid[0];

    $post['description'] = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWHTML );

    $model = $this->getModel('geoarticletag');

    if ($model->store($post)) {
      $msg = JText::_( 'Tag Saved' );
    } else {
      $msg = JText::sprintf( 'Error Saving Tag: %s', $model->getError() );
    }

    // Check the table in so it can be edited.... we are done with it anyway
    $model->checkin();
    $link = 'index.php?option=com_geoarticle';
    $this->setRedirect($link, $msg);
  }

  function cancel()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    // Checkin the weblink
    $model = $this->getModel('geoarticletag');
    $model->checkin();

    $this->setRedirect( 'index.php?option=com_geoarticle' );
  }
    

  function orderup()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $model = $this->getModel('geoarticletag');
    $model->move(-1);

    $this->setRedirect( 'index.php?option=com_geoarticle');
  }

  function orderdown()
  {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $model = $this->getModel('geoarticletag');
    $model->move(1);

    $this->setRedirect( 'index.php?option=com_geoarticle');
  }
  
}
